package com.mikufufu.blog.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mikufufu.blog.common.entity.AjaxResult;
import com.mikufufu.blog.model.dto.api.CategoryParam;
import com.mikufufu.blog.model.entity.Category;
import com.mikufufu.blog.model.vo.admin.CategoryVO;
import com.mikufufu.blog.service.CategoryService;
import com.mikufufu.blog.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
@Api(tags = "客户端的分类接口")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/detail")
    @ApiOperation(value = "获取分类详情", notes = "通过文章分类id获取详细的分类")
    @ApiImplicitParam(name = "id", value = "分类id", required = true)
    public  AjaxResult<Category> detail(Integer id) {
        return AjaxResult.data(categoryService.getById(id));
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页获取分类列表",notes = "分页获取分类列表")
    public AjaxResult<IPage<Category>> page(CategoryParam param) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(param.getType())){
            queryWrapper.eq(Category::getCategoryType,param.getType());
        }
        return AjaxResult.data(categoryService.page(param.getPage(),queryWrapper));
    }

    @GetMapping("/tree")
    @ApiOperation(value = "获取分类列表（树形结构）", notes = "获取分类列表（树形结构）")
    public AjaxResult<List<CategoryVO>> tree(Integer type) {
        return AjaxResult.data(categoryService.getCategoryTree(type));
    }

}
